<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col class="left" :span="3">
        <nuxt-link to="/">
          <img
            src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png"
            alt="美团"
          >
        </nuxt-link>
      </el-col>

      <el-col class="center" :span="17">
        <div class="wrapper" @keyup.enter="enter">
          <el-input
            v-model="search"
            placeholder="搜索商家或地点"
            @focus="focus"
            @blur="blur"
            @input="input"
          />
          <button class="el-button el-button--primary" @click="toList">
            <i class="el-icon-search" />
          </button>
          <dl v-if="isHotPlace" class="hotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item,index) in hotPlace" :key="index">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
          <dl v-if="isSearchList" class="searchList">
            <dd v-for="(item,index) in searchList" :key="index">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <a
            v-for="(item,idx) in hotPlace"
            :key="idx"
            :href="'/products?keyword='+encodeURIComponent(item.name)"
          >{{ item.name }}</a>
        </p>
        <ul class="nav">
          <li>
            <a href="http://waimai.meituan.com" class="takeout">
              美团外卖
            </a>
          </li>
          <li>
            <a href="//maoyan.com" class="movie">
              猫眼电影
            </a>
          </li>
          <li>
            <a href="//hotel.meituan.com" class="hotel">
              美团酒店
            </a>
          </li>
          <li>
            <a
              href="//minsu.meituan.com/?phx_wake_up_type=mtpc_category&phx_wake_up_source=nav_bar"
              class="apartment"
            >
              民宿/公寓
            </a>
          </li>
          <li>
            <a
              href="https://ecom.meituan.com/bizsettle/settle/merchantsSettle?utm_source=mtxz"
              class="business"
            >
              商家入驻
            </a>
          </li>
          <li>
            <a href="//gongyi.meituan.com/" class="business">
              美团公益
            </a>
          </li>
        </ul>
      </el-col>
      <el-col class="right" :span="4">
        <ul class="security">
          <li>
            <i class="refund" />
            <p class="txt">
              随时退
            </p>
          </li>
          <li>
            <i class="single" />
            <p class="txt">
              不满意免单
            </p>
          </li>
          <li>
            <i class="overdue" />
            <p class="txt">
              过期退
            </p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash'
import cookies from 'vue-cookies'
export default {
  data () {
    return {
      // 输入框的内容
      search: '',
      // 是否聚焦
      isFocus: false,
      hotPlace: [],
      searchList: [],
      city: ''
    }
  },
  computed: {
    isHotPlace () {
      return this.isFocus && !this.search
    },
    isSearchList () {
      return this.isFocus && this.search
    }
  },
  mounted () {
    this.getHotPlace()
  },
  methods: {
    focus () {
      this.isFocus = true
    },
    blur () {
      setTimeout(() => {
        this.isFocus = false
      }, 200)
    },
    // 用lodash库中的debounce延迟函数
    input: _.debounce(async function () {
      this.searchList = []
      const { status, data: { top } } = await this.$axios.get('/search/getSearch', {
        params: {
          input: this.search,
          city: this.city.replace('市', '')
        }
      })
      if (status === 200) {
        this.searchList = top
      } else {
        this.searchList = []
      }
    }, 300),
    // 获取热门搜索
    async getHotPlace () {
      this.city = cookies.get('city') ? cookies.get('city') : this.$store.state.geo.position.city
      const { status, data: { result } } = await this.$axios.get('/search/getHotPlace', {
        params: {
          city: this.city.replace('市', '')
        }
      })
      if (status === 200) {
        this.hotPlace = result.slice(0, 6)
      } else {
        this.hotPlace = []
      }
    },
    // 点击搜索触发
    toList () {
      // 判断是否输入内容
      if (this.search === '') {
        this.$message.warning('请输入内容!!!')
        return
      }
      location.href = `/products?keyword=${encodeURIComponent(this.search)}`
    },
    enter () {
      this.toList()
    }
  },
  // eslint-disable-next-line vue/order-in-components
  watch: {
    $route () {
      this.getHotPlace()
    }
  }
}
</script>

<style></style>
